package com.page.demo.dao.impl;

import com.page.demo.dao.UserDao;
import com.page.demo.pojo.User;
import com.page.demo.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    @Override
    public User queryUserByType(User user) {
        QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
        User query = null;
        try {
            String sql = "select * from t_user where username = ? and password  = ? and type = ?";
            Object[] params = {user.getUsername(), user.getPassword(), user.getType()};
            query = queryRunner.query(sql, new BeanHandler<>(User.class), params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return query;
    }

    @Override
    public int insertUser(User user) throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        String sql = "insert into t_user values (?,?,?,?,?,?,?,?)";
        Object[] params = {user.getId(), user.getUsername(), user.getPassword(), user.getSex(), user.getIdNumber(), user.getTel(), user.getAddress(), user.getType()};
        int update = queryRunner.update(JDBCUtils.getConnection(), sql, params);
        return update;
    }
    
    
}
